
    //$("a[name='collect']").prev().val()
    
    $(function () {
        displayCollection();
        $(".carbg a:even").live("click",function () {
            var id = $(this).parent().find(".id").val();
            if(localStorage.length == 0){
                if(confirm("请先登录")){
                    window.location.href = "Login.html";
                }
            }else if(localStorage.length>0){
                var userName = localStorage.getItem("user");
                $.ajax({
                    url : "easybuy/product/collect",
                    dataType : "json",
                    data : {"id" : id,"userName":userName},
                    type : "get",
                    success : function (data) {
                        if(data.result =="success"){
                            alert("收藏成功");
                            displayCollection();
                        }else if(data.exist=="exist"){
                            alert("您已收藏成功");
                        }
                    },
                    error : function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("出现错误");
                        console.log(XMLHttpRequest.status);
                        console.log(XMLHttpRequest.readyState);
                        console.log(textStatus);
                    }
                });

            }

        });
    });

function displayCollection() {
    var userName = localStorage.getItem("user");
    $.getJSON("easybuy/product/showCollect",
        {"userName":userName},
        function (data) {
            $(".l_history ul").html("");
            for (var i = 0; i < data.length; i++) {
                $(".l_history ul").append("<li>\n" +
                    "                    <div class=\"img\"><a href=\"#\"><img src=\"images/"+data[i].fileName+"\" width=\"185\" height=\"162\" /></a></div>\n" +
                    "                \t<div class=\"name\"><a href=\"#\">"+data[i].name+"</a></div>\n" +
                    "                    <div class=\"price\">\n" +
                    "                    \t<font>￥<span>"+data[i].price+"</span></font> &nbsp; 18R\n" +
                    "                    </div>\n" +
                    "                </li>");
            }
        })
}